/**
 * 
 */
package com.jinmei.mail.util;

import org.apache.commons.lang.StringUtils;

import com.jinmei.mail.oauth.OAuthTokenAccess;
import com.jinmei.mail.response.AccessTokenResponse;
import com.jinmei.utils.JinmeiConsts;
import com.jinmei.utils.ResourceManager;

/**
 * @author richard
 *
 */
public class ExmailUtils {
	
	public static String getLatestAccessToken() throws Exception {
		String accessToken = ResourceManager.getValue(JinmeiConsts.MAIL_LATEST_ACCESS_TOKEN);
		if (StringUtils.isEmpty(accessToken)) {
			refreshAccessToken();
		}
		return accessToken;
	}
	
	public static void refreshAccessToken() throws Exception {
		AccessTokenResponse response = null;
		while (response == null || StringUtils.isEmpty(response.getAccessToken())) {
			response = (AccessTokenResponse) OAuthTokenAccess.requestAccessToken();
		}
		ResourceManager.setValue(JinmeiConsts.MAIL_LATEST_ACCESS_TOKEN, response.getAccessToken());
	}

	public static String getLatestVersion() throws Exception {
		String version = ResourceManager.getValue(JinmeiConsts.MAIL_LATEST_VERSION);
		if (StringUtils.isEmpty(version)) {
			return JinmeiConsts.MAIL_VERSION_VALUE_0;
		}
		return version;
	}
	
}
